package com.sanchunhui.psc.ui.activity

import android.os.Bundle
import android.util.Log

import com.sanchunhui.psc.R
import com.sanchunhui.psc.common.BaseApplication
import com.sanchunhui.psc.common.BaseConstant
import com.sanchunhui.psc.data.protocol.ResponseInfo
import com.sanchunhui.psc.ext.onClick
import com.sanchunhui.psc.injection.component.DaggerUserComponent
import com.sanchunhui.psc.injection.module.UserModule
import com.sanchunhui.psc.presenter.UserInfoPresenter
import com.sanchunhui.psc.presenter.view.UserInfoView
import com.sanchunhui.psc.utils.GlideUtils
import kotlinx.android.synthetic.main.activity_user_info.*
import org.devio.takephoto.model.TResult
import org.jetbrains.anko.toast
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.MediaType
import java.io.File


/*
    用户信息
 */
class UserInfoActivity : BaseTakePhotoActivity<UserInfoPresenter>(), UserInfoView {

//    private val mUploadManager:UploadManager by lazy { UploadManager() }

    private var mLocalFileUrl:String? = null
    private var mRemoteFileUrl:String? = null

    private var mUserIcon:String? = null
    private var mUserName:String? = null
    private var mUserMobile:String? = null
//    private var mUserGender:String? = null
//    private var mUserSign:String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_user_info)

        initView()
        initData()
    }

    /*
        初始化视图
     */
    private fun initView() {
        //点击头像
        mUserIconView.onClick {
            showAlertView()
        }

//        mHeaderBar.getRightView().onClick {
//            mPresenter.editUser(mRemoteFileUrl!!,
//                    mUserNameEt.text?.toString()?:"",
//                    if(mGenderMaleRb.isChecked) "0" else "1")
//        }
    }

    /*
        初始化数据
     */
    private fun initData() {
        val user = BaseApplication.mUser
        mUserName = user.name
        mUserMobile = user.phone
//        mUserGender = AppPrefsUtils.getString(ProviderConstant.KEY_SP_USER_GENDER)
//        mUserSign = AppPrefsUtils.getString(ProviderConstant.KEY_SP_USER_SIGN)
        GlideUtils.loadUrlImage(this@UserInfoActivity, BaseConstant.IMAGE_SERVER_ADDRESS_URL+"${user.id%1000}/${user.id}.jpg",mUserIconIv)
        mUserNameEt.setText(mUserName)
        mUserMobileTv.text = mUserMobile

//        if (mUserGender == "0") {
//            mGenderMaleRb.isChecked = true
//        }
//        else {
//            mGenderFemaleRb.isChecked = true
//        }

//        mUserSignEt.setText(mUserSign)

    }

    /*
        Dagger注册
     */
    override fun injectComponent() {
        DaggerUserComponent.builder().activityComponent(mActivityComponent).userModule(UserModule()).build().inject(this)
        mPresenter.mView = this
    }

    /*
        获取图片成功回调
     */
    override fun takeSuccess(result: TResult?) {
        mLocalFileUrl = result?.image?.compressPath
        val file = File(mLocalFileUrl)
        val user = BaseApplication.mUser
        val fileBody  = RequestBody.create(MediaType.parse("image/jpg"), file)
        val photoPart  = MultipartBody.Part.createFormData("head",file.name, fileBody)
        val token = RequestBody.create(MediaType.parse("text/plain"),user.token)
        // 执行上传用户头像接口
        mPresenter.uploadUserHead(token, photoPart)
    }

    override fun onError(text: String) {
        super.onError(text)
        toast(text)
    }

    /*
        获取上传凭证回调
     */
    override fun onGetUploadTokenResult(result: String) {
        Log.e("result", result)
//        mUploadManager.put(mLocalFileUrl,null,result,object:UpCompletionHandler{
//            override fun complete(key: String?, info: ResponseInfo?, response: JSONObject?) {
//                mRemoteFileUrl = BaseConstant.IMAGE_SERVER_ADDRESS + response?.get("hash")
//
//                Log.d("test", mRemoteFileUrl)
//                GlideUtils.loadUrlImage(this@UserInfoActivity, mRemoteFileUrl!!,mUserIconIv)
//            }
//
//        },null)
    }

    /*
      上传头像回调
     */
    override fun onGetUploadHeadResult(result: ResponseInfo) {
        if (result.code == 0) {
            val user = BaseApplication.mUser
//            Picasso.with(this).load(BaseConstant.IMAGE_SERVER_ADDRESS_URL+"${user.id%1000}/${user.id}.jpg").into(mUserIconIv)
            runOnUiThread {
                GlideUtils.loadUrlImage(this@UserInfoActivity, BaseConstant.IMAGE_SERVER_ADDRESS_URL+"${user.id%1000}/${user.id}.jpg",mUserIconIv)
            }
        } else {
            toast("上传头像失败")
        }
    }

    /*
        编辑用户资料回调
     */
    override fun onEditUserResult(result: ResponseInfo) {
        toast("修改成功")
//        UserPrefsUtils.putUserInfo(result)
    }
}
